//
//  MainTabBarViewController.swift
//  weibo1
//
//  Created by 李进 on 15/10/14.
//  Copyright © 2015年 李进. All rights reserved.
//

import UIKit

class MainTabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //渲染tabbar的颜色
        tabBar.tintColor = UIColor.orangeColor()

        //加载自控制器
        addChildViewController()
        
        //加载按钮
        setupAddbut()
        
       
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //看完堆栈运行情况看出，这个生命周期是在加载完main控制器后才调用所以可以在这里将but带到最上面
        tabBar.bringSubviewToFront(addBut)
        
    }
    
    
    
    private lazy var addBut: UIButton = {
    
    let but = UIButton()
    
    but.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
    
    but.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
    but.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
    but.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
    but.sizeToFit()
    
    return but
    
    }()
    

}

//MARK:设置界面
extension MainTabBarViewController{
    
    private func setupAddbut(){
        
        tabBar.addSubview(addBut)
        
        //遍历tabbar的子控件
        let itemCount = childViewControllers.count
        
        //计算每个item的宽度
        let w = tabBar.frame.width / CGFloat(itemCount) - 1
        
        //设置按钮的frame
        
        addBut.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
        
        addBut.addTarget(self, action: "addButClick", forControlEvents: UIControlEvents.TouchUpInside)
        
    }
    
    //点击事件
    func addButClick(){
        //print("要跳转了")
        let vc:UIViewController
        //先判断永福是否有登入
        if userAccViewModel.shareAccViewModel.isLogin {
            vc = SendNewsViewController()
            vc.view.backgroundColor  = UIColor.whiteColor()
        }else{
            vc = WBLgjumupViewController()
        }
        
        let nav = UINavigationController(rootViewController: vc)
        
        presentViewController(nav, animated: true, completion: nil)
    }
    /// 加载自控制器
    private func addChildViewController() {
        
        addChildViewController(HomeTableViewController(), title: "首页", iconNmae: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", iconNmae: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoveryTableViewController(), title: "发现", iconNmae: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", iconNmae: "tabbar_profile")
    }
    
    /// 设置tabbar以及navgation
    ///
    /// - parameter vc:       tableview控制器
    /// - parameter title:    控制器标题
    /// - parameter iconNmae: tabbaritem图片名称
    private func addChildViewController(vc: UITableViewController, title: String, iconNmae: String) {
        
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: iconNmae)
        
        //设置导航控制器
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
    }
    
    
}